import 'package:base_flutter/base_flutter.dart';
import 'package:base_package_init/net/common_apis.dart';
import 'package:saas_order_meal/net/apis.dart';
import 'package:saas_order_meal/net/net_response/elder_bind_info.dart';
import 'package:saas_order_meal/net/net_response/elder_month_record.dart';
import 'package:saas_order_meal/net/net_response/elser_week_bind_info.dart';
import 'package:saas_order_meal/net/net_response/fee_rule_data.dart';
import 'package:saas_order_meal/saas_order_meal.dart';

import '../net/net_response/customer_tag_data.dart';

class WeekFoodModel extends BaseMvvmModel {
  @override
  void onCleared() {}

  ///获取长者月点餐列表
  Future<ElderMonthRecord> getElderMonth(
      {dynamic elderHumanId, dynamic startDate, dynamic showDay}) async {
    var map = {
      "elderHumanId": elderHumanId,
      "startDate": startDate,
      "showDay": showDay
    };

    var data = await HttpGo.instance.postData(Apis.elderMonthRecord,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return ElderMonthRecord.fromJson(data['data']);
    } else {
      throw data['msg'];
    }
  }

  ///获取长者点餐绑定信息
  Future<ElderBindInfo> getElderBindInfo(
      dynamic elderHumanId, String date) async {
    var map = {
      "elderHumanId": elderHumanId,
      "date": date,
    };
    var data = await HttpGo.instance.postData(Apis.elderBindInfo,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return ElderBindInfo.fromJson(data['data']);
    } else {
      throw data['msg'];
    }
  }

  ///获取周菜单-个人点餐信息-绑定场所，用餐方式；
  Future<ElderWeekBindInfo> getElderWeekBindInfo(
      dynamic elderHumanId, String date) async {
    var map = {
      "elderHumanId": elderHumanId,
      "date": date,
    };
    var data = await HttpGo.instance.postData(Apis.elderInfoByWeek,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return ElderWeekBindInfo.fromJson(data['data']);
    } else {
      throw data['msg'];
    }
  }

  ///根据日期获取周菜单
  Future<Map<String, dynamic>> getWeekMenuByDay(
      {required String siteId,
      required String date,
      String timeSection = "",
      required dynamic elderHumanId,
      dynamic orderModelType = "4"}) async {
    var map = {
      "siteId": siteId,
      "date": date,
      "timeSection": timeSection,
      "elderHumanId": elderHumanId,
      "orderModelType": orderModelType,
    };
    var data = await HttpGo.instance.postData(Apis.weekMenuByDay,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      var list = data['data'] as List?;
      if (list == null || list.isEmpty) {
        return {"menuList": []};
      } else {
        var type = list.first['weekMenuModel'];
        return {"type": type, "menuList": list, "menuId": list.first["menuId"],"menuTypeId": list.first["menuTypeId"]};
      }
    } else {
      throw data['msg'];
    }
  }

  Future<Map> submitWeekByMeal(
      {required dynamic elderHumanId,
      required dynamic siteId,
      required String useAddr,
      dynamic totalMoney,
      dynamic useType,
      required List<dynamic> goodsList,
      required String useDate,
      required dynamic useTimeSection,
      required dynamic menuId,dynamic userRoomId}) async {
    var map = {
      "elderHumanId": elderHumanId,
      "orderDate": DateTime.now()
          .toDateStr([yyyy, '-', mm, '-', dd, ' ', HH, ':', nn, ':', ss]),
      "siteId": siteId,
      "useNum": 1,
      "remark": "",
      "useAddr": useAddr,
      // "payment": "1",
      "isWeek": "1",
      "discountMoney": 0.0,
      "totalMoney": totalMoney,
      "orderModelType": 4,
      "useType": useType,
      "goodsList": goodsList,
      "useDate": "${useDate} 00:00:00",
      "useTimeSection": useTimeSection,
      "menuId": menuId,
      "dataFrom": "2",
      "saveFrom":"工作app",
      "userRoomId":userRoomId
    };

    var data = await HttpGo.instance.postData(Apis.submitWeekByMeal,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  ///批量提交一周订单
  Future<Map> submitWeekOrder(
      {required dynamic oldId, required List<dynamic> orderBeansList}) async {
    var map = {
      "userId": oldId,
      "orderBeansList": orderBeansList,
      "dataFrom": "2"
    };
    var data = await HttpGo.instance.postData(Apis.batchSubmitOrder,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return data;
    } else {
      throw data['msg'];
    }
  }

  Future<Map> oneCardPay(
      {required String orderId,
      String payment = "1",
      String type = "1",
      required String totalMoney,
      required dynamic actualMoney}) async {
    var map = {"id": orderId, "type": type, "dataFrom": "2"};
    if (!payment.isNullOrEmpty()) {
      map['payment'] = payment;
    }
    if (!totalMoney.isNullOrEmpty()) {
      map['totalMoney'] = totalMoney;
    }
    map['actualMoney'] = actualMoney;
    var result = await HttpGo.instance.postData(Apis.oneCardPay,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == result["code"]) {
      return result;
    } else {
      throw result['msg'];
    }
  }

  Future<Map> oneCardBatchPay(
      {dynamic orderCodeBatch,
      dynamic payment = 1,
      dynamic type = 1,
      required dynamic elderHumanId}) async {
    var map = {
      "orderCodeBatch": orderCodeBatch,
      "type": type,
      "payment": payment,
      "elderHumanId": elderHumanId,
      "dataFrom": "2"
    };
    var result = await HttpGo.instance.postData(Apis.batchPayOrder,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == result["code"]) {
      return result;
    } else {
      throw result['msg'];
    }
  }

  ///获取订单详情
  Future<void> getOrderDetail(dynamic id) async {
    var map = {"id": id};
    var data = await HttpGo.instance.postData(Apis.orderInfo,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
    } else {
      throw data['msg'];
    }
  }

  ///取消点餐
  Future<Map> cancelFood(dynamic id) async {
    var map = {"id": id, "dataFrom": "2"};
    var data = await HttpGo.instance.postData(Apis.cancelFood,
        data: map.toRequestStr(),
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      var bean = map;
      return bean;
    } else {
      throw data['msg'];
    }
  }

  ///获取长者标签
  Future<List<CustomerTagData>> getCustomerTag(dynamic elderId) async {
    var map = {"elderId": elderId};
    var data = await HttpGo.instance.postData(Apis.oldTag,
        data: map,
        options: Options(contentType: Headers.formUrlEncodedContentType));
    if ("0" == data['code']) {
      return ((data['data']['goodsTagList'] as List) ?? [])
          .map((e) => CustomerTagData.fromJson(e))
          .toList();
    } else {
      throw data['msg'];
    }
  }

  Future<FeeRuleData> getFeeRule(dynamic elderHumanId) async {
    var map = {
      "elderHumanId":elderHumanId
    };
    var data = await HttpGo.instance.postData(Apis.feeRule,data: map.toJsonStr(),options: Options(contentType: Headers.formUrlEncodedContentType));
    if("0"==data['code']){
      return FeeRuleData.fromJson(data['data']);
    }else{
      throw data['msg'];
    }
  }
}
